/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.gob.pnp.actions;

import com.opensymphony.xwork2.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts2.interceptor.*;
import org.apache.struts2.util.*;

/**
 *
 * @author Javero
 */
public class SubirImagen extends ActionSupport implements ServletContextAware, ServletRequestAware{
    private ServletContext servletContext;
    private HttpServletRequest servletRequest;
    

    @Override
    public String execute(){
        String ruta=servletContext.getRealPath("/");
        System.out.printf("!!!! ruta: %s\n",ruta);
        File archivo=new File(ruta+"imagen.png");
        FileOutputStream fos;
        try{
            InputStream in=servletRequest.getInputStream();
            int b;
            byte[] buf=new byte[1024];
            fos=new FileOutputStream(archivo);
            while((b=in.read(buf)) != -1){
                fos.write(buf);
            }
            fos.close();
            in.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }


        return SUCCESS;
    }


    /**
     * @param servletContext the servletContext to set
     */
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    /**
     * @param servletRequest the servletRequest to set
     */
    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

}
